package com.sphd.restaurantsystem.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.widget.TextView;

import com.sphd.restaurantsystem.R;
import com.sphd.restaurantsystem.adapters.MyViewPagerAdapter;
import com.sphd.restaurantsystem.base.BaseActivity;
import com.sphd.restaurantsystem.fragments.RechargeFragment;
import com.sphd.restaurantsystem.info.RestaurantInfo;
import com.sphd.restaurantsystem.interfaces.RechargeCallBack;
import com.sphd.restaurantsystem.utils.Constant;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/*
* 日、月份 充值
* */
public class RechargeActivity extends BaseActivity implements RechargeCallBack {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.account_recharge)
    TextView accountRecharge;
    @BindView(R.id.tabs)
    TabLayout tabs;
    @BindView(R.id.my_vp)
    ViewPager myVp;
    List<Fragment> fragmentList;
    MyViewPagerAdapter myViewPagerAdapter;
    String titles[] = {"", "", ""};
    Intent intent;
    Bundle bundle;
    RestaurantInfo restaurantInfo;
    int type;
    double account=0;
    String dayTop;
    String sunTop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recharge);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
       toolbar.setNavigationOnClickListener(v -> finish());

    }

    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();
        restaurantInfo = (RestaurantInfo) bundle.getSerializable(Constant.RESTAURANTSINFO);
        type=bundle.getInt(Constant.TYPE);
        account=bundle.getDouble(Constant.ACCOUNT);
        dayTop=bundle.getString(Constant.DAYTOP);
        sunTop=bundle.getString(Constant.SUNTOP);
        setTitleBar(toolbar,"充值记录");
        titles[0]=dayTop;
        titles[1]=sunTop;
        accountRecharge.setText("剩余充值总金额:" + turn(account,Constant.DECIMAL_POINT_TWO));

        fragmentList = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            RechargeFragment rechargeFragment = new RechargeFragment();
            rechargeFragment.setArguments(bundle);
            rechargeFragment.setIdType(restaurantInfo.getId(), i,this);
            fragmentList.add(rechargeFragment);
        }

        myViewPagerAdapter = new MyViewPagerAdapter(getSupportFragmentManager(), fragmentList, titles);
        myVp.setAdapter(myViewPagerAdapter);
        tabs.setupWithViewPager(myVp);
//        myVp.setOffscreenPageLimit(3);
        myVp.setCurrentItem(type);
    }

    @Override
    public void allMoney(double allMoney) {
        titles[2]= turn(allMoney,Constant.DECIMAL_POINT_TWO);
        myViewPagerAdapter.notifyDataSetChanged();
    }
}
